﻿using AnQinManagermentBackendAPI.Common.DateBase;
using AnQinManagermentBackendAPI.Common.Request;
using AnQinManagermentBackendAPI.Model.DTO.Staff;
using AnQinManagermentBackendAPI.Model.Entity;
using AnQinManagermentBackendAPI.Model.View.Staff;
using Microsoft.EntityFrameworkCore;

namespace AnQinManagermentBackendAPI.Service
{
    public class StaffService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 构造注入
         */
        public StaffService(DataContext context)
        {
            _context = context;
        }

        /**
         * 获取员工列表
         */
        public async Task<RequestResponseModel<PageResult<StaffView>>> GetStaffList(StaffSearchDTO staffSearchDTO)
        {
            //筛选
            var query = _context.StaffView
                .WhereIF(a => a.Code.Contains(staffSearchDTO.Code), !string.IsNullOrEmpty(staffSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(staffSearchDTO.Name), !string.IsNullOrEmpty(staffSearchDTO.Name))
                .WhereIF(a => a.Contact.Contains(staffSearchDTO.Contact), !string.IsNullOrEmpty(staffSearchDTO.Contact))
                .WhereIF(a => a.IsEnable == staffSearchDTO.IsEnable, staffSearchDTO.IsEnable != null)
                .WhereIF(a => a.DepartmentId == staffSearchDTO.DepartmentId, !string.IsNullOrEmpty(staffSearchDTO.DepartmentId))
                ;

            //返回列表
            var staffList = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(staffSearchDTO.CurrentPage, staffSearchDTO.PageSize)
                .ToListAsync();
            //总数据量
            var pageCount = await query.CountAsync();

            var pageResult = new PageResult<StaffView>(staffList, pageCount);

            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增员工列表
         */
        public async Task<RequestResponseModel<string>> AddStaff(StaffAddDTO staffAddDTO)
        {
            //查找重复账号
            var query = await _context.StaffView.FirstOrDefaultAsync(a => a.Account == staffAddDTO.Account);
            if (query != null)
            {
                return RequestResponse.CreateResponse(400, "账号已重复！", staffAddDTO.Account);
            }

            //操作数据
            DateTime date = DateTime.Now;
            Guid guid = Guid.NewGuid();

            //生成新数据
            var staff = new StaffModel
            {
                Id = guid.ToString(),
                Code = staffAddDTO.Code,
                Name = staffAddDTO.Name,
                Sex = staffAddDTO.Sex,
                Age = staffAddDTO.Age,
                Account = staffAddDTO.Account,
                Password = staffAddDTO.Password,
                Birthday = staffAddDTO.Birthday,
                Contact = staffAddDTO.Contact,
                Wechat = staffAddDTO.Wechat,
                Email = staffAddDTO.Email,
                DepartmentId = staffAddDTO.DepartmentId,
                IsEnable = staffAddDTO.IsEnable,
                CreateTime = date
            };

            //新增员工
            _context.Staff.Add(staff);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "新增成功", guid.ToString());
        }

        /**
         * 编辑员工
         */
        public async Task<RequestResponseModel<string>> PutStaff(StaffUpdateDTO staffUpdateDTO)
        {
            //查找数据
            var query = await _context.Staff.SingleOrDefaultAsync(a => a.Id == staffUpdateDTO.Id);

            //校验数据
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", staffUpdateDTO.Id);
            };

            //生成新数据
            query.Name = staffUpdateDTO.Name;
            query.Sex = staffUpdateDTO.Sex;
            query.Age = staffUpdateDTO.Age;
            query.Account = staffUpdateDTO.Account;
            query.Password = staffUpdateDTO.Password;
            query.Birthday = staffUpdateDTO.Birthday;
            query.Contact = staffUpdateDTO.Contact;
            query.Wechat = staffUpdateDTO.Wechat;
            query.Email = staffUpdateDTO.Email;
            query.DepartmentId = staffUpdateDTO.DepartmentId;
            query.IsEnable = staffUpdateDTO.IsEnable;


            //更新员工表
            _context.Staff.Update(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", staffUpdateDTO.Id);
        }

        /**
         * 删除员工
         */
        public async Task<RequestResponseModel<string>> DeleteStaff(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.Staff.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //删除操作
            _context.Staff.Remove(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 删除员工 - 批量
         */
        public async Task<RequestResponseModel<List<string>>> DeleteStaffMulti(List<string> Ids)
        {
            //判空
            if (Ids == null || Ids.Count == 0)
            {
                return RequestResponse.CreateResponse(400, "Ids为空", new List<string>());
            }

            //查找id
            var query = await _context.Staff
                .Where(a => Ids.Contains(a.Id))
                .ToListAsync();

            //是否有匹配资源
            if (query.Count == 0)
            {
                return RequestResponse.CreateResponse(400, "Ids无效", Ids);
            }

            //操作数据库
            _context.Staff.RemoveRange(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Ids);
        }

        /**
         * 启用员工 - 批量
         */
        public async Task<RequestResponseModel<List<string>>> EnableStaff(List<string> Ids)
        {
            //判空
            if (Ids == null || Ids.Count == 0)
            {
                return RequestResponse.CreateResponse(400, "Ids为空", new List<string>());
            }

            //查找id
            var query = await _context.Staff
                .Where(a => Ids.Contains(a.Id))
                .ToListAsync();

            //是否有匹配资源
            if (query.Count == 0)
            {
                return RequestResponse.CreateResponse(400, "Ids无效", Ids);
            }

            //设置启用
            query.ForEach(a =>
            {
                a.IsEnable = true;
            });

            //设置数据库
            _context.Staff.UpdateRange(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "启用成功", Ids);
        }

        /**
         * 禁用员工 - 批量
         */
        public async Task<RequestResponseModel<List<string>>> DisableStaff(List<string> Ids)
        {
            //判空
            if (Ids == null || Ids.Count == 0)
            {
                return RequestResponse.CreateResponse(400, "Ids为空", new List<string>());
            }

            //查找id
            var query = await _context.Staff
                .Where(a => Ids.Contains(a.Id))
                .ToListAsync();

            //是否有匹配资源
            if (query.Count == 0)
            {
                return RequestResponse.CreateResponse(400, "Ids无效", Ids);
            }

            //设置启用
            query.ForEach(a =>
            {
                a.IsEnable = false;
            });

            //设置数据库
            _context.Staff.UpdateRange(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "禁用成功", Ids);
        }
    }
}
